import React from 'react';
import { message } from 'antd';
export default {
    messageTips(res) {
        if (res.code === 0) {
            message.success(res.msg);
        } else if (res.code === 2002) {
            message.error(res.msg);
        } else if (res.code === 4010 || res.code === 404) {
            message.warning(res.msg);
        }
    },
    formateDate(time) {
        if (!time) return "";
        let date = new Date(time);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let min = date.getMinutes();
        let sec = date.getSeconds();
        if (month < 10)
            month = '0' + month;
        if (day < 10)
            day = '0' + day;
        if (hours < 10)
            hours = '0' + hours;
        if (min < 10)
            min = '0' + min;
        if (sec < 10)
            sec = '0' + sec;
        return year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec;
    },
    pagination(page, callOnChange, callOnShowSizeChange) {
        return {
            onChange: (current) => {
                callOnChange(current)
            },
            onShowSizeChange: (current, pageSize) => {
                callOnShowSizeChange(current, pageSize);
            },
            current: page.pageNumber,   //当前页数
            pageSize: page.pageSize, //每页条数
            pageSizeOptions: ['10', '15', '20', '30'], //指定每页可以显示多少条
            showQuickJumper: true,   //是否可以快速跳转至某页
            showSizeChanger: true,   //是否可以改变 pageSize
            total: page.totalRow, //数据总数
            showTotal: () => {
                return `共 ${page.totalRow} 条`
            },
        }
    },
    // 隐藏身份证号中11位
    formatIdentity(number) {
        number += '';
        return number.replace(/(\d{3})\d*(\d{4})/g, '$1***********$2')
    },
    //处理对象参数值，排除对象参数值为”“、null、undefined，并返回一个新对象
    FilterParam,
    //数组元素的上下移动  上移swapItems(arr, $index, $index - 1); 下移swapItems(arr, $index, $index + 1);
    swapItems(arr, index1, index2) {
        arr[index1] = arr.splice(index2, 1, arr[index1])[0];
        return arr;
    },
    //删除左右两端的空格
    delSpace(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    /*产生随机数的函数*/
    validateCode(n) {
        //验证码可能包含的字符
        let s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        let ret = ""; //用于保存生成的验证码
        //利用循环,随机产生生成验证码的每一个字符
        for (var i = 0; i < n; i++) {
            var index = Math.floor(Math.random() * 62); //随机产生一个0~62之间的数字
            ret += s.charAt(index);//将随机产生的数字当作字符串的位置下标,在字符串s中取出该字符，并加入到ret中
        }
        return ret; //返回产生的验证码
    },
    //图片Base64处理
    getBase64(img, callback) {
        const reader = new FileReader();
        reader.addEventListener('load', () => callback(reader.result));
        reader.readAsDataURL(img);
    }
};


//处理对象参数值，排除对象参数值为”“、null、undefined，并返回一个新对象
function FilterParam(obj) {
    function dataType(obj) {
        if (obj === null) return "Null";
        if (obj === undefined) return "Undefined";
        return Object.prototype.toString.call(obj).slice(8, -1);
    }
    let param = {};
    if (obj === null || obj === undefined || obj === "") return param;
    for (var key in obj) {
        if (dataType(obj[key]) === "Object") {
            param[key] = FilterParam(obj[key]);
        } else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
            param[key] = obj[key];
        }
    }
    return param;
}